package com.bdqn.controller;

import com.bdqn.LayUI.Layui;
import com.bdqn.Service.Enquire.EnquireService;
import com.bdqn.Service.IdMapping.IdMappingService;
import com.bdqn.Service.Stock.StockService;
import com.bdqn.entity.Enquire;
import com.bdqn.entity.Stock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Calendar;
import java.util.List;

@Controller
@RequestMapping("/Enquire")
public class EnquireController {
    @Autowired
    EnquireService enquireService;
    @Autowired
    private IdMappingService idMappingService;
    @Autowired
    private StockService StockService;

    @RequestMapping("/Sel")
    @ResponseBody
    public Enquire Select(int id,String stockName){
        //创建询价书对象假数据
        Enquire enquire=new Enquire();
        Calendar calendar=Calendar.getInstance();
        //询价书编号为1000+年月日秒
        enquire.setEnquireNum("1000"+calendar.get(Calendar.YEAR)+calendar.get(Calendar.MONTH)+calendar.get(Calendar.DATE)+calendar.get(Calendar.HOUR_OF_DAY)+calendar.get(Calendar.MINUTE)+calendar.get(Calendar.SECOND));
        //将假数据添加至数据库
        enquireService.Insert(enquire);
        //关联表修改
        enquireService.UpdateIdMapping(id,enquire);
        //查询
        Enquire select = enquireService.Select(enquire);
        return select;
    }

    @RequestMapping("/all")
    @ResponseBody
    public Layui<Enquire> findAllByPage(Integer page, Integer limit, Enquire enquire){
        page = (page-1)*limit;
        List<Enquire> enquires = enquireService.findAll(enquire,page,limit);
        int count = enquireService.getCount();
        Layui<Enquire> enquireLayui=new Layui<Enquire>();
        enquireLayui.setCode(0);
        enquireLayui.setData(enquires);
        enquireLayui.setCount(count);
        return enquireLayui;
    }
    @RequestMapping("/all1")
    @ResponseBody
    public Layui<Enquire> findAllByPage1(Integer page, Integer limit, Enquire enquire){
        enquire.setStatus1("未申请");
        page = (page-1)*limit;
        List<Enquire> enquires = enquireService.findAll(enquire,page,limit);
        int count = enquireService.getCount();
        Layui<Enquire> enquireLayui=new Layui<Enquire>();
        enquireLayui.setCode(0);
        enquireLayui.setData(enquires);
        enquireLayui.setCount(count);
        return enquireLayui;
    }

    @RequestMapping("/del")
    @ResponseBody
    public int deleteEnquire(Integer id){
        idMappingService.deleteIdM(id);
        return enquireService.deleteEnquire(id);
    }


    @RequestMapping("/updateEnquire")
    @ResponseBody
    public boolean updateEnquire(Enquire enquire,String stockName){
        Stock stock=new Stock();
        stock.setStockName(stockName);
        stock.setEnq(enquire.getEnquireName());
        StockService.UpdateByName(stock);
        enquire.setStatus("未发布");
        return enquireService.updateEnquire(enquire);
    }

    @RequestMapping("/findById")
    @ResponseBody
    public Enquire findById(Enquire enquire){
        Enquire select = enquireService.Select(enquire);
        return select;
    }

    @RequestMapping("/findMateSupEnq")
    @ResponseBody
    public Layui<Enquire> findMateSupEnq(Integer id){
        List<Enquire> mateSupByEnq = enquireService.findMateSupByEnq(id);
        Layui<Enquire> enquireLayui = new Layui<>();
        enquireLayui.setCode(0);
        enquireLayui.setData(mateSupByEnq);
        return enquireLayui;
    }

}
